package com.black.platform.user.service.bz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.common.web.PageQuery;
import com.black.platform.user.mapper.ShareUserMapper;
import com.black.platform.user.po.ShareUser;
import com.black.platform.user.service.bz.ShareUserService;
import com.black.common.util.ComCodeUtil;
import com.black.common.constant.ShareConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author linwenfeng
 */
@Service
@Slf4j
public class ShareUserServiceImpl extends ServiceImpl<ShareUserMapper, ShareUser> implements ShareUserService {

    @Override
    public IPage<ShareUser> search(PageQuery<ShareUser> searchVo) {
        ShareUser queryData = searchVo.getQueryData();
        List<String> comCodeList = null;
        if (!StringUtils.isEmpty(queryData.getComCode())) {
            comCodeList = Arrays.asList(queryData.getComCode().split(","));
        }
        return this.baseMapper.search(searchVo.getPager(), queryData.getValid(), queryData.getName(), queryData.getSerialNo(),queryData.getPlatform(), comCodeList);
    }

    @Override
    public ShareUser findUserByUsername(String username, boolean isIncludeInvalid) {
        QueryWrapper<ShareUser> query = new QueryWrapper<>();
        query.eq("username", username);
        if (!isIncludeInvalid) {
            query.eq("valid", "1");
        }
        return this.baseMapper.selectOne(query);
    }

    @Override
    public ShareUser findUserByUsername(String username) {
        return this.findUserByUsername(username, false);
    }

    @Override
    public int closeUser(ShareUser user) {
        user.setValid(ShareConstant.Status.IN_VALID);
        return this.baseMapper.updateById(user);
    }

    @Override
    public int updateIcon(Long userId, String iconPath) {
        return baseMapper.updateIcon(userId, iconPath);
    }


    @Override
    public List<ShareUser> findPowerUser(String comCode, String permissionCode) {
        if (StringUtils.isEmpty(comCode)) {
            comCode = null;
        } else {
            comCode = ComCodeUtil.getPowerComCode(comCode) + "%";
        }
        return this.baseMapper.findPowerUser(comCode, permissionCode);
    }
}
